import store from '@/store'
import { Message, Notification } from 'element-ui'

/**
 * 微信号二维码 ok
 */
function pullWeChatQrCodeTaskResultNoticeHandle(msg) {
    Message({
        type: 'success',
        message: '获取微信二维码成功！',
        duration: 1000
    })
    let qrcode = JSON.parse(msg.message)
    qrcode.ChatRoomId = -1
    store.dispatch('nedb/UpdateQrCode', qrcode)
}

/**
 * 新的朋友列表 ok
 */
function friendAddReqListNoticeHandle(msg) {
    let message = JSON.parse(msg.message)
    message.ModifyTime = new Date().getTime()
    if (!message.Requests) {
        message.Requests = []
    }
    // 数据存入nedb
    store.dispatch('nedb/UpdateOrAddNewFriends', message)
}

/**
 * 清理僵尸粉结果 ok
 */
function postFriendDetectCountNoticeHandle(msg) {
    let message = JSON.parse(msg.message)
    message.RspTime = new Date().getTime()
    // 数据存入nedb
    store.dispatch('nedb/AddOrUpdateZombies', message)
}

/**
 * 从服务端拉取自动任务列表 ok
 */
function autoTaskDetailListRespHandle(msg) {
    let messages = JSON.parse(msg.message)
    let dl = {}
    for (const message of messages) {
        if (dl[message.wechatId]) {
            dl[message.wechatId][message.auto_type] = message.state
        } else {
            dl[message.wechatId] = {}
            dl[message.wechatId][message.auto_type] = message.state
        }
    }
    store.commit('nedb/SET_AUTO_TASK_CONFIG', dl)
}

/**
 * 更新服务器自动任务 ok
 */
function autoTaskUpdateRespHandle(msg) {
    let message = JSON.parse(msg.message)
    let actionMap = {
        1001: '智能抢红包',
        1002: '自动通过好友请求',
        1003: '自动通过群邀请',
        1004: '自动收转账'
    }
    let result = message.state === 0 ? '打开' : '关闭'
    let str = result + actionMap[message.auto_type]
    Message({
        type: 'success',
        message: str,
        duration: 1000
    })
    // 重新拉取列表
    store.dispatch('websocket/AutoTaskDetailList')
}


/**
 * 新的朋友申请通知
 */
function friendAddReqeustNoticeHandle(msg) {
    let message = JSON.parse(msg.message)
    message.ReqTime = new Date().getTime()
    // 好友请求添加到数据库
    store.dispatch('nedb/UpdateNewFriendsByOne', message)
    // 新的朋友通知
    // store.dispatch('friend/SetNewFriendNotice', true)
    let wechats = store.getters['conversation/wechats']
    let wechatNick = message.WeChatId
    for (const wechat of wechats) {
        if (wechat.WeChatId === message.WeChatId) {
            wechatNick = wechat.WeChatNick
            break
        }
    }
    Notification({
        title: '提示',
        dangerouslyUseHTMLString: true,
        message: `<strong style="color:green;">${message.FriendNick}</strong> 请求加 <strong style="color:green;">${wechatNick}</strong> 为好友！`,
        type: 'warning',
        offset: 50,
        duration: 0
    });
}

/**
 * 通过号码查询联系人结果
 */
function findContactTaskResultNoticeHandle(msg) {
    let message = JSON.parse(msg.message)
    console.log('通过号码查询联系人结果', message)
    // Avatar: "http://wx.qlogo.cn/mmhead/ver_1/vibh3xCE4NiceA9cY1TuLIZqsTmnnKzpJgX4PlNEgCgb7f8jHE1mSFyMyafibdsxDgmwuAskw0xibACcNDKyfIt8svo9R5Gn8WUHXwQx3VouiciaU/96"
    // City: "杭州"
    // Gender: "Male"
    // IsFriend: true
    // NickName: "重阳"
    // Province: "浙江"
    // SearchText: "15268296911"
    // Success: true
    // UserName: "Uta2012"
    // WeChatId: "wxid_n5l57vd9fpf622"

    if (message.Success) {
        Message({
            type: 'success',
            message: '根据号码查询联系人成功！',
            duration: 1000
        })
    } else {
        Message({
            type: 'warning',
            message: '根据号码查询联系人失败！',
            duration: 1000
        })
    }
    let doc = {
        result: 1,
        Success: message.Success ? message.Success : false
    }
    for (const key in message) {
        if (message.hasOwnProperty(key)) {
            doc[key] = message[key];
        }
    }
    store.dispatch('nedb/UpdateFindContactTask', doc)
}


export default {
    pullWeChatQrCodeTaskResultNoticeHandle,
    friendAddReqListNoticeHandle,
    friendAddReqeustNoticeHandle,
    postFriendDetectCountNoticeHandle,
    autoTaskDetailListRespHandle,
    autoTaskUpdateRespHandle,
    findContactTaskResultNoticeHandle
}
